<? 
require_once '../includes/includes.php';
include ("seguridad.php");
conexion();

//importa las clases de las entidades
require '../clases/News.php';
require '../clases/Categories.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title><?php titulopanel();?></title>
<link rel="STYLESHEET" type="text/css" href="estilo.css">
<link type="text/css" href="../plugins/jquery_ui/css/smoothness/jquery-ui-1.8.13.custom.css" rel="Stylesheet" />   
<script type="text/javascript" src="../plugins/jquery.js"></script>
<link href="../plugins/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../plugins/uploadify/swfobject.js"></script>
<script type="text/javascript" src="../plugins/uploadify/jquery.uploadify.v2.1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('#file_upload').uploadify({
    'uploader'  : '../plugins/uploadify/uploadify.swf',
    'script'    : '../plugins/uploadify/uploadify.php',
    'cancelImg' : '../plugins/uploadify/cancel.png',
    'folder'    : '../images_gallery/',
	'simUploadLimit' : 3,
    'auto'      : true,
	'multi'     : true,	
	'scriptData'  : {'id': $("#id").val()},
	'buttonText': 'Seleccionar', 
  });
});
</script>
<script type="text/javascript" src="../plugins/jquery_ui/js/jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript" src="../plugins/jquery_validate/jquery.validate.js"></script>
<script type="text/javascript" src="../plugins/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<!-- EDITOR -->
<?php include_once 'includes/tinymce.html';?>
<!-- /TinyMCE --> 

<!-- datapicker -->
<script> 
	$(document).ready(function(){
		$("#created" ).datepicker({
			dateFormat: 'yy-mm-dd'
			// showOn: "button",
			// buttonImage: "images/calendar.ico",
			// buttonImageOnly: true

				});
	})
</script> 	
<script>
	$(function() {
		$( "input:submit, a, button", ".boton" ).button();		
	});
</script>

<!-- Errores jaquery -->
<script>
  $(document).ready(function(){
		
	$("#form").validate({
		rules: {			
			categorie: {
				required: true,
				},
			created: {
				required: true,
				},	
			title: {
				required: true,
					},
			summary: {
				required: true,
				},
			conten: {
					required: true,
					},	
		},		
		                
		messages: {
			categorie: {
				required: "Seleccione una categoria"
				
						},
			created: {
				required: "Seleccione la fecha"
			 			},			

            title:{
                required: "Coloque un título"
                    },
                    
            summary:{
                required:"Coloque un copete"        
                     },        
            conten: "Coloque un cuerpo"		
                    
			}
	});
	
  });

</script>
<!-- /ERRORES -->


<?php
if(isset($_POST[cargar]) or isset($_GET[id])){

	if(isset($_GET[id])){

		$id = $_GET[id];

		//Traigo la noticia con el id traido por get
		$con_n = mysql_fetch_array(mysql_query("SELECT * FROM news where id = $id "));		
		//echo "SELECT * FROM news where id = $id ";
		
		//se guarda los valores de la noticia 	
		//echo $con_n['title'];

		$categorie 	= $con_n['category_id'];
		$title 		= $con_n['title'];
		$summary 	= $con_n['summary'];
		$content 	= $con_n['content'];
		$published 	= $con_n['published'];
		// $created=$_POST['created']." ".date("H:i:s");			
	}

	if(isset($_POST[cargar])){
		//se carga una noticia 	
		$categorie = sqlint($_POST['categorie']);
		$title=sqlstring($_POST['title']);
		$summary=sqlstring($_POST['summary']);
		$content=sqlstring($_POST['conten']);
		$published=sqlint($_POST['published']);
		$created=$_POST['created']." ".date("H:i:s");	
	}
	
	//creo el objeto con los valores pasados
	$not = new News($categorie, $title, $summary, $content, $published, $created);
	
	if(isset($_POST[cargar]) and !isset($_GET[id])){
		$not->save();
	}
	else if(isset($_GET[id]) and isset($_POST[cargar])){
		$not->update($id);
		//echo $valores="user_id = '{$_SESSION['usuario']["idusuario"]}',category_id = '5', title = '$this->_title', summary = '$this->_summary',content = '$this->_content', id_published = $this->_published";	
	}

	//obtengo el ultimo id para cargar las imagenes
	$consulta = mysql_fetch_array(mysql_query("SELECT MAX(id) as id FROM news "));
 	$id_img = $consulta['id'];
 	
 	$_SESSION[id_news] = $id;

	

	if(isset($_POST[cargar])){
?>
<script>
	$(function() {
		$( "#tabs" ).tabs( { selected: 1 } );
	});
</script>
<?php
	}		
	else if(isset($_GET[id])){
		//$not->update();
?>
<script>
	$(function() {
		$( "#tabs" ).tabs( { selected: 0 } );
	});
</script>
<?php
}

}
else if(!isset($_POST[cargar]) or !isset($_GET[id])){

//no se carga nada 

?>

<script>
	$(function() {
		$( "#tabs" ).tabs( {disabled:[1,2]} );	
		
	});
	

</script>


<?php
}
?>


<script>
	function mostrarDialog() {
		$("#dialog").dialog({ height: 700, width: 900 });
		$("#dialog").load("mostrar_imagenes.php?id=<?php echo $id_img; ?>");
	};
</script>


</head>
<body>
<div id="contenedor">
  	<div id="encabezado"><?php include('header.php') ?></div><!--fin de encabezado-->
	
	<div id="content_center">
		
	<div id="menuizquierda"><?php include('menu.php') ?></div>	<!--fin de menuizquierda-->

	<div id="content">		
		<div id="sub_content_0">
			<div id="sub_content">
				<h2>Cargar Noticia</h2>
			</div>
		</div>	


			<div class="demo">

<div id="tabs">
	<ul>
		<li><a href="#tabs-1" id="tab">Cargar Noticia</a></li>
		<li><a href="#tabs-2" id="img">Cargar Imagenes</a></li>		
		
	</ul>
	
	<div id="tabs-1" style="height:740px">

		<div id="formulario">			
			<form class ="admin" id="form" name="form" action="#" method="post" enctype="multipart/form-data">
				<br>			
				<p>
					<table>
						<tr>
							<td >Publicar:</td>
							<td width="80"><input type="checkbox" name="published" value="1" checked /></td>

							<td>Fecha:</td>
							<td width="180"><input type="text" id="created" name="created" value="<?php echo date("Y-m-d")?>" style="width:100px;"/></td>

							<td>Categoria:</td>
							<td><select name="categorie" id="categorie">
														
								<option value ="">Elija categoria</option> 
								<?php

								$cat = new Categories();
								$cate = $cat->getAll();						

								print_r($cate);

								
								foreach ($cate as $categoria) {
									if($categoria['id'] == $categorie)
										$descrip = $categoria['description'];
								}

								if(isset($_POST[cargar]) or isset($_GET[id])){
									echo '<option value="'.$categorie.'" selected="selected">'.$descrip.'</option>';
								}

								
								
								foreach ($cate as $categoria) {
									echo '<option value="'.$categoria['id'].'">'.$categoria['description'].' </option>';
								}

								
								
								?>
							</select>
						</td>
						</tr>
					</table>				
				</p>				
				
				<p>
					<span>T&iacute;tulo:</span>				
					
					<input type="text" name="title" id="title" onkeyup="if (this.value.length == 139) this.disabled = true;" size="70px" value="<?php echo $title; ?>">
				</p>
				<br>
				<p>
					<span>Copete:</span>
					<textarea name="summary" id="summary" cols="60" rows="3"><?php echo $summary ?></textarea>
				</p>	
				<div style="clear:both"></div>
				
				<p>
					<span>Cuerpo:</span>				
					<textarea name="conten" id="conten" cols="60" rows="25" ><?php echo $content ?></textarea>
				</p>			

				<div style="clear:both"></div>
				<br>
				<input name="boton" type="hidden" id="boton" value="1" />
				
				<input name="cargar" id="cargar" type="submit" class="buttonadmin" value="Aceptar" <?php if(isset($_POST[cargar])) echo 'disabled';
				 ?>/>
				<input type="button" class="buttoncancelar"  value="<?php 
				if(isset($_POST[cargar]))
				echo 'Finalizar';
				else
				echo 'Cancelar';?>" onClick="location.href='noticias.php'">



				<br>
				
				
				</form>
				
			</div>	
	</div>

	<div id="tabs-2">

		<fieldset class="admin">
		
   			<label>Archivos a Subir:</label>
        <!-- Esta div contendrá todos los campos file que creemos -->
   				
			<input id="file_upload" name="file_upload" type="file" class="buttonadmin"/>
		<input id="id" name="id" type="hidden" value="<?php echo $id_img ?>" />
		<br/>
		<br/>
		<input type="button" class="buttoncancelar" value="Finalizar" onclick="location.href='noticias.php'"></a>
		<input type="button" class="buttonadmin" value="Ver" onclick="mostrarDialog()">
   		</fieldset>

   							
		<div id="dialog" style="display:none" title="Imagenes" style="height:100%">

   		</div>	
				

	</div>	

			
		
				
	
	
	</div><!--fin de content-->
</div><!--fin de content center-->

	<div id="piepagina"><?php include 'pie.php'; ?></div>	<!--fin de piepagina-->
</div>


</body>

</html>


